﻿using System;
using System.Collections.Generic;
using System.Data.Entity;

namespace Moonwish.Core.DataAccess
{
    public abstract class QueryCommand<T> where T : class
    {
        protected readonly DbContext DbContext;
        protected readonly Request Request;
        protected readonly Dictionary<string, string> Map = new Dictionary<string, string>();

        protected QueryCommand(DbContext dbContext, Request request)
        {
            DbContext = dbContext;

            Request = request;
        }

        /// <summary>
        /// 根据FieldName映射到数据库中的对应字段
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        protected string ToDbColumn(string column)
        {
            if (string.IsNullOrEmpty(column))
                throw new InvalidOperationException("查询条件的字段为空或者长度为零。");

            if (!Map.ContainsKey(column.ToLower()))
                throw new InvalidOperationException(string.Format("系统不支持按“{0}”条件进行查询。", column));

            return Map[column.ToLower()];
        }

        public abstract List<T> Execute();

        protected abstract string BuildSql();
    }
}
